<template>
  <div class="curr-page">
    <!-- 搜索框 -->
    <!-- 模糊搜索 -->
    <el-input
      v-model="searchData"
      placeholder="please input"
      size="medium"
      style="width:500px"
      @change="searchChange"
      class="page_search mt-5"
    >
      <template #append>
        <el-button icon="el-icon-search"></el-button>
      </template>
    </el-input>

    <!-- 分页管理 -->
    <!-- 按照时间排序 -->
    <!-- 论坛帖子是否显示描述，默认显示 -->
    <div>
      <!-- top bar opertion -->
      <div style="height:80px" class="d-flex jc-end mr-2 ai-center">
        <!-- 操作控制 -->
        <div class="fs-sm d-flex ai-center" style="color:#666">
          <el-tooltip content="simple order " placement="top" effect="light">
            <i
              class="el-icon-menu mr-2"
              :class="[!isShowAllDesc ? 'text-red' : '']"
              @click="isShowAllDesc = !isShowAllDesc"
            ></i>
          </el-tooltip>
          <el-tooltip content="show description" placement="top" effect="light">
            <i
              class="el-icon-files mr-4"
              :class="[isShowAllDesc ? 'text-red' : '']"
              @click="isShowAllDesc = !isShowAllDesc"
            ></i>
          </el-tooltip>
          <el-tooltip content="order by time" placement="top" effect="light">
            <i
              class="text-red mr-2"
              :class="[order ? ' el-icon-top' : 'el-icon-bottom']"
              @click="clickOrderByTime"
            ></i>
          </el-tooltip>
        </div>

        <!-- 分页信息 -->
        <el-pagination
          layout="total, prev, pager, next"
          @current-change="handleCurrentChange"
          :currentPage="pageInfo.currentPage"
          :page-size="pageInfo.pageSize"
          :total="pageInfo.total"
        >
        </el-pagination>
      </div>

      <!-- 讨论列表 -->
      <!-- 若无列表内容，则显示为空的 -->
      <el-empty :image-size="200" v-show="!discussionModel[0]"></el-empty>

      <!-- 显示列表 -->
      <div v-for="(item, index) in discussionModel" :key="index">
        <!-- title -->
        <div class="mb-3 text-nav">
          <span @click="scanArticle(item)">{{ item.title }}</span>
        </div>
        <!-- desc -->
        <div class="fs-sm mr-5 text-align" v-show="isShowAllDesc">
          <span v-show="item.desc" class=" textShowTo2">{{ item.desc }}</span>
          <span v-show="!item.desc">no description</span>
        </div>
        <!-- content -->
        <div class="d-flex ai-center mt-3">
          <!-- tags -->
          <el-tag
            :key="tag"
            v-for="tag in item.tags"
            :disable-transitions="false"
            tag="info"
            size="mini"
            class="mr-2"
          >
            {{ tag }}
          </el-tag>

          <!-- opertion -->
          <div class="fs-sm d-flex ai-center">
            <!-- 用户评论 点赞 -->
            <!-- 
            <i class="el-icon-edit" v-show="item.isAuthor"></i>
            <i class="el-icon-chat-line-round"></i>
            <span class="ml-1">10</span>
             -->
            <span class="mx-2">{{ item.updatedAt }}</span>
          </div>
        </div>
        <!-- divider -->
        <el-divider direction="horizontal" content-position="left"></el-divider>
      </div>
    </div>

    <!-- edit -->
    <!-- 新建帖子 -->
    <div class="d-flex flex-column mt-5">
      <!-- topbar -->
      <div class="mb-3 d-flex jc-end ai-center">
        <!-- save button  -->
        <div class="d-flex flex-1">
          <el-button
            type="primary"
            size="mini"
            @click="submitForm('articleForm')"
          >
            创建帖子
          </el-button>
        </div>

        <!-- clear icon -->
        <i class="el-icon-delete pr-3" @click="resetForm('articleForm')"></i>
      </div>

      <!-- form -->
      <!-- add new 帖子 -->
      <div class="pb-4">
        <div class="bg-olive border-radius-5 pr-3 py-3">
          <el-form
            label-position="right"
            label-width="100px"
            ref="articleForm"
            :model="articleForm"
            :rules="rules"
          >
            <!-- title -->
            <el-form-item prop="title" label="帖子标题 :">
              <el-input
                v-model="articleForm.title"
                style="width:250px"
                placeholder="请填写文章标题"
              ></el-input>
            </el-form-item>

            <!-- description -->
            <el-form-item prop="desc" label="帖子摘要 :">
              <el-input
                v-model="articleForm.desc"
                type="textarea"
                placeholder="请填写文章摘要"
                :autosize="{ minRows: 4, maxRows: 5 }"
              ></el-input>
            </el-form-item>

            <!-- tags -->
            <el-form-item label="标签 :">
              <div class="">
                <el-tag
                  :key="tag"
                  v-for="tag in articleForm.tags"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag)"
                  class="mr-2"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter="handleInputConfirm"
                  @blur="handleInputConfirm"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput"
                  >+ 新增</el-button
                >
              </div>
            </el-form-item>

            <!-- description -->
            <el-form-item label="">
              <!-- article info -->
              <vue-editor v-model="articleForm.content" class="editor" />
            </el-form-item>
          </el-form>
        </div>
        
      </div>
    </div>

    <!-- 显示具体discussion -->
    <el-dialog v-model="outerVisible" width="60%">
      <template #title>
        <div class=" text-align">{{ discussionForSee.title }}</div>
        <el-tag
          :key="d_tag"
          v-for="d_tag in discussionForSee.tags"
          :disable-transitions="false"
          tag="info"
          size="mini"
          class="mr-2 mt-3"
        >
          {{ d_tag }}
        </el-tag>
      </template>
      <!-- desc -->
      <el-alert
        title="Description:"
        type="success"
        effect="light"
        :closable="false"
        :description="discussionForSee.desc"
      ></el-alert>

      <div>
        <div v-html="discussionForSee.content"></div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="outerVisible = false" size="mini">
            Close</el-button
          >
        </div>
      </template>

      <!-- 内层 -->
      <el-dialog
        width="30%"
        title="内层 Dialog"
        v-model="innerVisible"
        append-to-body
      >
      </el-dialog>
    </el-dialog>

  </div>
</template>

<script>
import { VueEditor } from "vue3-editor";
export default {
  components: { VueEditor },
  data() {
    return {
      discussionModel: [],
      isShowAllDesc: true,
      order: false,
      searchData: "",

      // 分页信息
      pageInfo: {
        currentPage: 1,
        pageSize: 10,
        total: 1,
      },
      // 预览内容
      outerVisible: false,
      innerVisible: false,
      discussionForSee: {},

      /**
       * edit
       */
      articleForm: {
        title: "",
        desc: "",
        content: "",
        tags: [],
      },

      inputVisible: false,
      inputValue: "",

      // form rules
      // 表单验证规则
      rules: {
        title: [{ required: true, message: "标题不为空", trigger: "blur" }],
        desc: [
          {
            required: true,
            message: "摘要不为空",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    // 按照标题搜索
    searchChange() {
      console.log(this.searchData);
      this.handleCurrentChange(1);
    },

    // flash page data
    async handleCurrentChange(val) {
      const res = await this.$http.get("/discussion", {
        params: {
          currentPage: val ? val : 1,
          pageSize: this.pageInfo.pageSize,
          orderByTime: !this.order ? -1 : 1,
          searchKey: this.searchData,
        },
      });
      this.discussionModel = res.data.model;
      this.pageInfo.total = this.discussionModel.length;
    },

    // 按照时间排序
    clickOrderByTime() {
      this.order = !this.order;
      this.handleCurrentChange();
    },

    // scan
    // 浏览帖子
    scanArticle(item) {
      this.discussionForSee = {};
      this.discussionForSee = item;
      this.outerVisible = true;
    },

    /**
     * edit
     * 新建讨论页面
     */
    // 删除选中的tag
    handleClose(tag) {
      this.articleForm.tags.splice(this.articleForm.tags.indexOf(tag), 1);
    },

    // 显示tag输入框
    showInput() {
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    // 添加tag
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.articleForm.tags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },

    // submit discuss
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.articleForm.userId = sessionStorage.userId;
          this.$http.post("/discussion", this.articleForm);
          this.$notify({
            title: "保存成功",
            message: "",
            type: "success",
            duration: 1200,
          });
        } else {
          console.log("error submit!!");
          return false;
        }
        this.articleForm = {};
        // this.$router.go(0);
      });
    },

    // reset the forms
    // 清除表单内容
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$notify({
        title: "success clear",
        message: "",
        type: "success",
        duration: 1200,
      });
      this.articleForm = {};
    },
  },

  created() {
    this.handleCurrentChange();
  },
};
</script>

<style lang="scss" scoped>
.curr-page {
  margin: 0 5rem 5rem 5rem;
}
</style>
